package edu.rakovic.elearning.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;

public class CoursesBean implements Courses {
	
	private Map<String, Course> courses;
	
	public CoursesBean() {
		this(new HashMap<String, Course>());
	}

	public CoursesBean(Map<String, Course> courses) {
		super();
		this.courses = courses;
	}

	public Collection<Course> retreiveCourses() {
		return courses.values();
	}
	
	public Course save(Course course) {
		return courses.put(course.getName(), course);
	}
	
	public Course delete(String courseName) {
		return courses.remove(courseName);
	}
	
	public Course retreiveSingleCourse(String courseName) {
		return courses.get(courseName);
	}
	
	public Collection<Course> retrieveTeachingCourses(String userName) {
		ArrayList<Course> retval = new ArrayList<Course>();
		// Using foreach for filtering. If username of teacher from course 
		// matches one that we passed as method argument, course will be added
		// "retval" (retval is our return value).
		for (Course course : courses.values()) {
			if (course.getTeacher().getUserName().equalsIgnoreCase(userName)) {
				retval.add(course);
			}
		}
		return retval;
	}
		

}
